/*
 * @Description: 路由权限控制
 * @Author: 郑灶欣
 * @Date: 2021-12-17 11:08:51
 * @LastEditors: 郑灶欣
 * @LastEditTime: 2021-12-17 14:44:32
 * @FilePath: \vue3-admin\src\permission.js
 */
import router from "./router";
const whiteList = ["/404", "/login"];
import { getToken } from "@/utils/auth"; // get token from cookie
router.beforeEach(async (to, from, next) => {
  if (to.meta.title) {
    // 动态标题
    document.title = to.meta.title;
  }
  const hasToken = getToken();
  //能获取到token
  if (hasToken) {
    if (to.path === "/login") {
      next({ path: "/" });
    } else {
      next();
    }
  } else {
    //在白名单内
    if (whiteList.indexOf(to.path) !== -1) {
      next();
    } else {
      next(`/login`);
    }
  }
});

router.afterEach(() => {});
